home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / arc.zoo / arcmisc.c < prev    next >
C/C++ Source or Header  |  1991-08-13  |  9KB  |  509 lines

  1. /*
  2.  * Miscellaneous routines to get ARC running on non-MSDOS systems...
  3.  * $Header: arcmisc.c,v 1.8 88/07/31 18:50:56 hyc Exp $ 
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include "arc.h"
  9.  
  10. #if    MSDOS
  11. #include <dir.h>
  12. #include <stat.h>
  13. #endif
  14.  
  15. #if    GEMDOS
  16. #include <osbind.h>
  17. #include <stat.h>
  18. char           *index(), *rindex();
  19.  
  20. void 
  21. exitpause()
  22. {
  23.     while (Cconis())
  24.         Cnecin();
  25.     fprintf(stderr, "Press any key to continue: ");
  26.     fflush(stderr);
  27.     Cnecin();
  28.     fprintf(stderr, "\n");
  29. }
  30.  
  31. int
  32. chdir(dirname)
  33.     char           *dirname;
  34. {
  35.     char           *i;
  36.     int             drv;
  37.  
  38.     i = dirname;
  39.     if ((i = index(dirname, ':')) != NULL) {
  40.         drv = i[-1];
  41.         i++;        /* Move past device spec */
  42.         if (drv > '\'')
  43.             drv -= 'a';
  44.         else
  45.             drv -= 'A';
  46.         if (drv >= 0 && drv < 16)
  47.             Dsetdrv(drv);
  48.     }
  49.     if (*i != '\0')
  50.         return (Dsetpath(i));
  51. }
  52. #endif
  53.  
  54. #if    UNIX
  55. #include <sys/types.h>
  56. #if    MINIX
  57. #include <dirent.h>
  58. #define DIRECT dirent
  59. #else
  60. #ifndef SYSV
  61. #include <sys/dir.h>
  62. #endif
  63. #endif
  64. #include <sys/stat.h>
  65. #ifndef __STDC__
  66.     int    rename(), unlink();
  67. #endif
  68. #endif
  69.  
  70. #if    SYSV
  71. #include <dirent.h>
  72. #define DIRECT dirent
  73. #else
  74. #ifndef MINIX
  75. #define DIRECT direct
  76. #endif
  77. #endif
  78.  
  79. #ifndef __STDC__
  80. #if    BSD
  81. char    *
  82. memset(s, c, n)        /* oops. Thought it was standard BSD, but my Sun */
  83.     char    *s;    /* fooled me again. -- hyc */
  84.     int    c, n;
  85. {
  86.     register int i;
  87.     for(i=0;i<n;i++)
  88.         s[i]=c;
  89.     return(s);
  90. }
  91. #endif
  92. #endif
  93.  
  94. #ifndef __STDC__
  95. char           *strcpy(), *strcat(), *malloc();
  96. int             strlen(), strcmp();
  97. #endif
  98. int match();
  99.  
  100. int
  101. move(oldnam, newnam)
  102.     char           *oldnam, *newnam;
  103. {
  104. #ifndef __STDC__
  105.     FILE           *fopen();
  106. #endif
  107.     FILE           *old, *new;
  108. #if    !MTS
  109.     struct stat     oldstat;
  110. #endif
  111. #ifndef __STDC__
  112.     char           *strcpy();
  113. #endif
  114.     void        filecopy();
  115. #if    GEMDOS
  116.     if (Frename(0, oldnam, newnam))
  117. #else
  118.     if (rename(oldnam, newnam))
  119. #endif
  120. #if    !MTS
  121.     {
  122.         if (stat(oldnam, &oldstat))    /* different partition? */
  123.             return (-1);
  124.         old = fopen(oldnam, OPEN_R);
  125.         if (old == NULL)
  126.             return (-1);
  127.         new = fopen(newnam, OPEN_W);
  128.         if (new == NULL)
  129.             return (-1);
  130.         filecopy(old, new, oldstat.st_size);
  131.         return(unlink(oldnam));
  132.     }
  133.     return 0;
  134. #else
  135.     return(-1);
  136. #endif
  137. }
  138.  
  139. static void
  140. _makefn(source, dest)
  141.     char           *source;
  142.     char           *dest;
  143. {
  144.     int             j;
  145. #if    MSDOS
  146.     char           *setmem();
  147. #else
  148. #ifndef __STDC__
  149.     char           *memset();
  150. #endif
  151. #endif
  152.  
  153.     setmem(dest, 17, 0);    /* clear result field */
  154.     for (j = 0; *source && *source != '.'; ++source)
  155.         if (j < 8)
  156.             dest[j++] = *source;
  157.     for (j = 9; *source; ++source)
  158.         if (j < 13)
  159.             dest[j++] = *source;
  160. }
  161. /*
  162.  * make a file name using a template 
  163.  */
  164.  
  165. char           *
  166. makefnam(rawfn, template, result)
  167.     char           *rawfn;    /* the original file name */
  168.     char           *template;    /* the template data */
  169.     char           *result;    /* where to place the result */
  170. {
  171.     char            et[17], er[17], rawbuf[STRLEN], *i, *rindex();
  172.  
  173.     *rawbuf = 0;
  174.     strcpy(rawbuf, rawfn);
  175. #if    MTS
  176.     i = rawbuf;
  177.     if (rawbuf[0] == tmpchr[0]) {
  178.         i++;
  179.         strcpy(rawfn, i);
  180.     } else
  181. #endif
  182.     if ((i = rindex(rawbuf, CUTOFF))) {
  183.         i++;
  184.         strcpy(rawfn, i);
  185.     }
  186. #if    DOS
  187.     else if ((i = rindex(rawbuf, ':'))) {
  188.         i++;
  189.         strcpy(rawfn, i);
  190.     }
  191. #endif
  192.     if (i)
  193.         *i = 0;
  194.     else
  195.         *rawbuf = 0;
  196.  
  197.     _makefn(template, et);
  198.     _makefn(rawfn, er);
  199.     *result = 0;        /* assure no data */
  200.     strcat(result, rawbuf);
  201.     strcat(result, er[0] ? er : et);
  202.     strcat(result, er[9] ? er + 9 : et + 9);
  203.     return ((char *) &result[0]);
  204. }
  205.  
  206. #if    MSDOS || SYSV || MINIX
  207.  
  208. int
  209. alphasort(dirptr1, dirptr2)
  210.     struct DIRECT **dirptr1, **dirptr2;
  211. {
  212.     return (strcmp((*dirptr1)->d_name, (*dirptr2)->d_name));
  213. }
  214.  
  215. #endif
  216.  
  217. void
  218. upper(string)
  219.     char           *string;
  220. {
  221.     char           *p;
  222.  
  223.     for (p = string; *p; p++)
  224.         if (islower(*p))
  225.             *p = toupper(*p);
  226. }
  227. /* VARARGS1 */
  228. void
  229. abort(s, arg1, arg2, arg3)
  230.     char           *s;
  231. {
  232.     fprintf(stderr, "ARC: ");
  233.     fprintf(stderr, s, arg1, arg2, arg3);
  234.     fprintf(stderr, "\n");
  235. #if    UNIX
  236.     perror("UNIX");
  237. #endif
  238. #if    GEMDOS
  239.     exitpause();
  240. #endif
  241.     exit(1);
  242. }
  243.  
  244. #if    !MTS
  245.  
  246. char           *
  247. gcdir(dirname)
  248.     char           *dirname;
  249.  
  250. {
  251. #ifndef __STDC__
  252.     char           *getwd();
  253. #endif
  254. #if    GEMDOS
  255.     int             drv;
  256.     char           *buf;
  257. #endif
  258.     if (dirname == NULL || strlen(dirname) == 0)
  259.         dirname = (char *) malloc(1024);
  260.  
  261. #if    !GEMDOS
  262.     getwd(dirname);
  263. #else
  264.     buf = dirname;
  265.     *buf++ = (drv = Dgetdrv()) + 'A';
  266.     *buf++ = ':';
  267.     Dgetpath(buf, 0);
  268. #endif
  269.     return (dirname);
  270. }
  271.  
  272. #if    UNIX
  273. char           *pattern;    /* global so that fmatch can use it */
  274. #endif
  275.  
  276. char           *
  277. dir(filename)        /* get files, one by one */
  278.     char           *filename;    /* template, or NULL */
  279. {
  280. #if    GEMDOS
  281.     static int      Nnum = 0;
  282.     static DMABUFFER dbuf, *saved;
  283.     char           *name;
  284.  
  285.     if (Nnum == 0) {    /* first call */
  286.         saved = (DMABUFFER *) Fgetdta();
  287.         Fsetdta(&dbuf);
  288.         if (Fsfirst(filename, 0) == 0) {
  289.             name = malloc(FNLEN);
  290.             strcpy(name, dbuf.d_fname);
  291.             Nnum++;
  292.             return (name);
  293.         } else {
  294.             Fsetdta(saved);
  295.             return (NULL);
  296.         }
  297.     } else {
  298.         if (Fsnext() == 0) {
  299.             name = malloc(FNLEN);
  300.             strcpy(name, dbuf.d_fname);
  301.             return (name);
  302.         } else {
  303.             Nnum = 0;
  304.             Fsetdta(saved);
  305.             return (NULL);
  306.         }
  307.     }
  308. }
  309. #else
  310.     static struct DIRECT **namelist;
  311.     static char   **NameList;
  312.     static char    namecopy[STRLEN], *dirname;
  313. #if    UNIX
  314.     int             alphasort();
  315.     int             scandir();
  316. #endif                /* UNIX */
  317.     int             fmatch();
  318.     static int      Nnum = 0, ii;
  319. #ifndef __STDC__
  320.     char        *rindex();
  321.     int        free();
  322. #endif    
  323.  
  324.  
  325.     if (Nnum == 0) {    /* first call */
  326.         strcpy(namecopy,filename);
  327.         if(pattern=rindex(namecopy,CUTOFF)) {
  328.             *pattern = 0;
  329.             pattern++;
  330.             dirname = namecopy;
  331.         } else {
  332.             pattern = filename;
  333.             dirname = ".";
  334.         }
  335.         Nnum = scandir(dirname, &namelist, fmatch, alphasort);
  336.         NameList = (char **) malloc(Nnum * sizeof(char *));
  337.         for (ii = 0; ii < Nnum; ii++) {
  338.             (NameList)[ii] = malloc(strlen(namelist[ii]->d_name) + 1);
  339.             strcpy((NameList)[ii], namelist[ii]->d_name);
  340.         }
  341.         ii = 0;
  342.     }
  343.     if (ii >= Nnum) {    /* all out of files */
  344.         if (Nnum) {    /* there were some files found */
  345.             for (ii = 0; ii < Nnum; ii++)
  346.                 free(namelist[ii]);
  347.             free(namelist);
  348.         }
  349.         Nnum = 0;
  350.         return (NULL);
  351.     } else {
  352.         return ((NameList)[ii++]);
  353.     }
  354. }
  355.  
  356. /*
  357.  * Filename match - here, * matches everything 
  358.  */
  359.  
  360. int
  361. fmatch(direntry)
  362.     struct DIRECT  *direntry;
  363. {
  364.     char           *string;
  365.  
  366.     string = direntry->d_name;
  367.  
  368.     if (!strcmp(pattern, "") || !strcmp(pattern, "*.*") || !strcmp(pattern, "*"))
  369.         return (1);
  370.     return (match(string, pattern));
  371. }
  372. #endif                /* GEMDOS */
  373. #else
  374. /* dir code for MTS under Bell Labs C... */
  375.  
  376. char           *
  377. dir(filepattern)
  378.     char           *filepattern;    /* template or NULL */
  379. {
  380. #ifndef __STDC__
  381.     char           *malloc(), *index();
  382. #endif
  383. #if    USECATSCAN
  384.     fortran void    catscan(), fileinfo();
  385.  
  386.     struct catname {
  387.         short           len;
  388.         char            name[257];
  389.     }               pattern;
  390.  
  391.     struct catval {
  392.         int             maxlen;
  393.         int             actlen;
  394.         char            name[257];
  395.     }               catreturn;
  396.  
  397.     char           *i;
  398.     int             j, RETCODE;
  399.  
  400.     static int      catptr = 0;
  401.     static int      catflag = 0x200;
  402.     static int      cattype = 1;
  403.     static int      patflag = 0;
  404.  
  405.     catreturn.maxlen = 256;
  406.  
  407.     if (patflag) {
  408.         patflag = 0;
  409.         catptr = 0;
  410.         return (NULL);
  411.     }
  412.     if (filepattern) {
  413.         strcpy(pattern.name, filepattern);
  414.         pattern.len = strlen(filepattern);
  415.         if (!index(filepattern, '?'))
  416.             patflag = 1;
  417.     }
  418.     if (patflag) {
  419.         fileinfo(&pattern, &cattype, "CINAME  ", &catreturn, _retcode RETCODE);
  420.         catptr = RETCODE ? 0 : 1;
  421.     } else
  422.         catscan(&pattern, &catflag, &cattype, &catreturn, &catptr);
  423.  
  424.     if (!catptr)
  425.         return (NULL);
  426.     else {
  427.         char           *k;
  428.  
  429.         k = index(catreturn.name, ' ');
  430.         if (k)
  431.             *k = 0;
  432.         else {
  433.             j = catreturn.actlen;
  434.             catreturn.name[j] = 0;
  435.         }
  436.         k = catreturn.name;
  437.         if (catreturn.name[0] == tmpchr[0])
  438.             k++;
  439.         else if ((k = index(catreturn.name, sepchr[0])))
  440.             k++;
  441.         else
  442.             k = catreturn.name;
  443.         j = strlen(k);
  444.         i = malloc(++j);
  445.         strcpy(i, k);
  446.         return (i);
  447.     }
  448. #else
  449.     fortran void    gfinfo();
  450.     static char     gfname[24];
  451.     static char     pattern[20];
  452.     static int      gfdummy[2] = {
  453.                       0, 0
  454.     },              gfflags;
  455.     int             i, RETCODE;
  456.     char           *j, *k;
  457.  
  458.     if (filepatt